/*
 * @Author: fhw
 * @Date: 2022-07-17 13:29:17
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-01-12 15:27:38
 * @Description:
 */
import { NestFactory } from '@nestjs/core'
import { DocumentBuilder, SwaggerDocumentOptions, SwaggerModule } from '@nestjs/swagger'
import { AdminModule } from './admin.module'
import { ValidationPipe } from './admin.pipe'
import { TransformInterceptor } from './common/transform.interceptor'
import { HttpExceptionFilter } from './http-exception.filter'
import helmet from 'helmet';
import * as csurf from 'csurf';
import * as cookieParser from 'cookie-parser'
import { join } from 'path'
import * as serveStatic from 'serve-static';
import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';
import * as express from 'express';
import { ExpressAdapter } from '@nestjs/platform-express'


// const httpsOptions = {
//   ca: fs.readFileSync('./src/common/cert/text.fhuvue.cn.csr'),
//   key: fs.readFileSync('./src/common/cert/text.fhuvue.cn.key'),
//   cert: fs.readFileSync('./src/common/cert/text.fhuvue.cn_bundle.crt'),
// }
async function bootstrap() {
  // const server = express();
  const app = await NestFactory.create(AdminModule)
  //   全局过滤器
  app.useGlobalFilters(new HttpExceptionFilter())
  // 参数校验
  app.useGlobalPipes(new ValidationPipe())
  app.useGlobalInterceptors(new TransformInterceptor())
  const config = new DocumentBuilder().addBearerAuth()// 开启 BearerAuth 授权认证
    .setTitle('个人博客')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('文章', 'article')
    .addTag('文章分类', 'articleCategory')
    .addTag('文章标签', 'articleTag')
    .addTag('文章评论', 'articleComment')
    .addTag('用户', 'chatUser')
    .addTag('群聊', 'groupChat')
    .addTag('用户群聊表', 'chatUserGroup')
    .addTag('系统消息', 'sysMessage')
    .addTag('用户好友', 'chatUserFriends')
    .addTag('简历', 'resume')
    .addTag('留言板', 'messageBoard')
    .addTag('聊天记录', 'chatRecord')
    .addTag('图片上传', 'upload')
    .addTag('历史会话', 'historySession')
    .build()
  const document = SwaggerModule.createDocument(app, config)
  // fs.writeFileSync("./swagger-spec.json", JSON.stringify(document));
  SwaggerModule.setup('swagger', app, document)
  // 门路前缀：如：http://www.test.com/api/v1/user
  app.setGlobalPrefix('');
  //cors：跨域资源共享，形式一：容许跨站拜访
  app.enableCors();
  //避免跨站脚本攻打
  app.use(helmet());
  //CSRF爱护：跨站点申请伪造
  // app.use(cookieParser());
  // app.use(csurf({ cookie: true }));
  app.use('/public', serveStatic(join(__dirname, '../public'), {
    maxAge: '1d',
    extensions: ['jpg', 'jpeg', 'png', 'gif'],
  }));
  await app.listen(3002)
  // await app.init();
  // http.createServer(server).listen(3003);
  // https.createServer(httpsOptions, server).listen(3002);
  // process.env.user_list = {}
  console.log('https://localhost:3002/swagger')
}
bootstrap()
